package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.PatientExam;
import com.woniu.mapper.PatientExamMapper;
import com.woniu.service.PatientExamService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import com.woniu.vo.PatientExamVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class PatientExamServiceImpl implements PatientExamService {
    @Resource
    private PatientExamMapper patientExamMapper;
    @Override
    public ResponseUtil findAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PatientExamVO> examList = patientExamMapper.findAllVO();
        PageInfo<PatientExamVO> pageInfo = new PageInfo<>(examList);
        return ResponseUtil.get(ResponseEnum.OK, pageInfo);
    }

    @Override
    public ResponseUtil selectByCondition(String patientName, String itemName, Integer statusInt, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PatientExamVO> examList = patientExamMapper.selectByCondition(patientName,itemName, statusInt);
        PageInfo<PatientExamVO> pageInfo = new PageInfo<>(examList);
        return ResponseUtil.get(ResponseEnum.OK, pageInfo);
    }

    @Override
    public ResponseUtil completeExam(Integer id, String result,Integer statusInt) {
        System.out.println("=== 完成检查参数 ===");
        System.out.println("id: " + id);
        System.out.println("result: " + result);
        System.out.println("statusInt: " + statusInt);

        PatientExam updateRecord = new PatientExam();
        updateRecord.setId(id);
        updateRecord.setStatus(statusInt); // 2-已完成
        updateRecord.setResult(result);
        updateRecord.setFinishTime(new Date());

        System.out.println("更新记录: " + updateRecord);

        int rows = patientExamMapper.updateByPrimaryKeySelective(updateRecord);

        System.out.println("影响行数: " + rows);

        if (rows == 1) {
            return ResponseUtil.get(ResponseEnum.OK, "检查完成");
        } else {
            return ResponseUtil.get(ResponseEnum.FAIL, "更新失败");
        }
    }

    @Override
    public void syncStatusFromCkDetail() {
        // 同步ck_detail状态到patient_exam
        patientExamMapper.syncPatientExamStatus();

    }
}
